package com.ly.juc;
import	java.util.concurrent.Callable;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Classname TestCallable
 * @Description
 * FutureTask类 实现 runable接口,传入callable进行构造,futurtask的run方法中在调用callable的call方法
 * @Date 2020/10/4 15:55
 * @Author 冷心影翼
 */
public class TestCallable {


	public static void main(String[] args) throws ExecutionException, InterruptedException {
		callable();
	}

	public static void callable() throws ExecutionException, InterruptedException {
		FutureTask<Integer> task1= new FutureTask<>(new Calc());
		//futureTask有缓存.
		new Thread(task1).start();
		new Thread(task1).start();

		final int[] number = {0};

		Calc calc = new Calc();
		FutureTask<Integer> task2 = new FutureTask<>(()->{
			calc.number ++;
			return calc.number;
		});
		for(int i=0;i<10;i++) {
			new Thread(task2).start();
		}
		System.out.println(task2.get());
		System.out.println(calc.number);
	}
}


class Calc implements Callable<Integer>{

	public int number = 0;

	@Override
	public Integer call() throws Exception {
		number++;
		System.out.println(1);
		return number;
	}
}

